<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2011 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  2012 Andreas Koob 
 * @author     Andreas Koob 
 * @package    piwik_analyser 
 * @license    LGPL 
 * @filesource
 */


/**
 * Class piwik_analyser_module_exampleapi
 *
 * @copyright  2012 Andreas Koob 
 * @author     Andreas Koob 
 * @package    Controller
 */
class piwik_analyser_module_exampleapi extends frontend
{
	private $functions;
	
	public function __construct()
	{
		$this->import('Database');
		$this->import('Input');
		$this->functions = new piwik_analyser_functions();
	}
	
	public function getFunctions($currentFunction)
	{
		$strFunctions='';
		$strFunctions.='<option value="exampleapi.getpiwikversion"';if($currentFunction=='exampleapi.getpiwikversion'){$strFunctions.=' selected="selected"';}$strFunctions.='>getPiwikVersion</option>';
		$strFunctions.='<option value="exampleapi.getanswertolife"';if($currentFunction=='exampleapi.getanswertolife'){$strFunctions.=' selected="selected"';}$strFunctions.='>getAnswerToLife</option>';
		$strFunctions.='<option value="exampleapi.getobject"';if($currentFunction=='exampleapi.getobject'){$strFunctions.=' selected="selected"';}$strFunctions.='>getObject</option>';
		$strFunctions.='<option value="exampleapi.getsum"';if($currentFunction=='exampleapi.getsum'){$strFunctions.=' selected="selected"';}$strFunctions.='>getSum</option>';
		$strFunctions.='<option value="exampleapi.getnull"';if($currentFunction=='exampleapi.getnull'){$strFunctions.=' selected="selected"';}$strFunctions.='>getNull</option>';
		$strFunctions.='<option value="exampleapi.getdescriptionarray"';if($currentFunction=='exampleapi.getdescriptionarray'){$strFunctions.=' selected="selected"';}$strFunctions.='>getDescriptionArray</option>';
		$strFunctions.='<option value="exampleapi.getcompetitiondatatable"';if($currentFunction=='exampleapi.getcompetitiondatatable'){$strFunctions.=' selected="selected"';}$strFunctions.='>getCompetitionDatatable</option>';
		$strFunctions.='<option value="exampleapi.getmoreinformationanswertolife"';if($currentFunction=='exampleapi.getmoreinformationanswertolife'){$strFunctions.=' selected="selected"';}$strFunctions.='>getMoreInformationAnswerToLife</option>';
		$strFunctions.='<option value="exampleapi.getmultiarray"';if($currentFunction=='exampleapi.getmultiarray'){$strFunctions.=' selected="selected"';}$strFunctions.='>getMultiArray</option>';
		return $strFunctions;
	}
	
	public function getQueryCode($function)
	{
		switch($function)
		{
			case 'exampleapi.getpiwikversion':
				return '';//No code required
				break;
			case 'exampleapi.getanswertolife':
				return '';//No code required
				break;
			case 'exampleapi.getobject':
				return '';//No code required
				break;
			case 'exampleapi.getsum':
				$strCode.='<legend>Number A:</legend><input type="text" name="a" class="tl_text" /><br />';
				$strCode.='<legend>Number B:</legend><input type="text" name="b" class="tl_text" /><br />';
				return $strCode;
				break;
			case 'exampleapi.getnull':
				return '';//No code required
				break;
			case 'exampleapi.getdescriptionarray':
				return '';//No code required
				break;
			case 'exampleapi.getcompetitiondatatable':
				return '';//No code required
				break;
			case 'exampleapi.getmoreinformationanswertolife':
				return '';//No code required
				break;
			case 'exampleapi.getmultiarray':
				return '';//No code required
				break;
			default:
				break;
		}
	}
	
	public function processFunction($function)
	{
		switch($function)
		{
			case 'exampleapi.getpiwikversion':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getPiwikVersion';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>'',
					'answer'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getanswertolife':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getAnswerToLife';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>'',
					'answer'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getobject':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getObject';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>'',
					'answer'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getsum':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getSum';
				$url.='&a=' . $this->Input->post('a');
				$url.='&b=' . $this->Input->post('b');
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>'',
					'answer'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getnull':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getNull';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>'',
					'answer'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getdescriptionarray':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getDescriptionArray';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getcompetitiondatatable':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getCompetitionDatatable';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getmoreinformationanswertolife':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getMoreInformationAnswerToLife';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			case 'exampleapi.getmultiarray':
				$account=$this->functions->getAccountDetails($this->Input->post('account'));
				$url=$account['url'];
				$url.='/?module=API';
				$url.='&method=ExampleAPI.getMultiArray';
				$url.='&format=php';
				$url.='&token_auth=' . $account['token'];
				$fetched = file_get_contents($url);
				$arrResult=array(
					'query'=>$url,
					'result'=>unserialize($fetched)
				);
				return $arrResult;
				break;
			default:
				break;
		}
	}
}

?>